package com.live.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;






import org.apache.log4j.Logger;

import com.google.gson.Gson;
import com.live.model.Admin;
import com.live.service.AdminService;
import com.live.util.DesTool;
import com.live.util.StringUtil;


@WebServlet("/adminGetAccount")
public class AdminController extends HttpServlet {
	private final Logger logger = Logger.getLogger(AdminController.class);
	private static final long serialVersionUID = 1L;
	private AdminService adminService;
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.info("执行登陆doPost方法");
		String code = request.getParameter("code").trim();
		String password = request.getParameter("password").trim();
		String account = request.getParameter("account").trim();
		HttpSession session = request.getSession(true);
		try {
			String sessionCode = (String)session.getAttribute(CodeController.IMG_CODE);//获取session中的验证码
			String str = "";
			String errInfo = "";
			if (sessionCode.equalsIgnoreCase(code)) {
					adminService = new AdminService();
					Admin admin  = adminService.getAdmin(account, DesTool.encode(password));
					System.out.println(password);
					if(admin != null) {
						session.setAttribute("CurrentUser", admin);
						admin.setId(admin.getId());
						admin.setLandingTime(StringUtil.getStrDateTime());
						adminService.editAdminLandingTime(admin);
						errInfo = "success";
						logger.debug("------getAdmin  getAdmin = " + account);
					} else {
						errInfo = "usererror";		
					}
				}
				else {
					errInfo = "codeerror";				 	//验证码输入有误
			}
			Gson gson=new Gson();
			str = gson.toJson(errInfo);
			//关于response的必要设置
			response.setCharacterEncoding("utf-8");
			response.setContentType("application/json;charset=utf-8");
			response.setHeader("pragma","no-cache");
			response.setHeader("cache-control","no-cache");
			//传输JSON
			PrintWriter out = response.getWriter();
			out.println(str);
			out.flush();
		} catch (Exception e) {
			e.printStackTrace();
			logger.debug("------执行登陆doPost方法  执行登陆doPost方法 = " + e.getMessage());
		}		
		
	}	
	
}


